home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / strcls10.zip / STR.CPP < prev    next >
C/C++ Source or Header  |  1991-04-06  |  4KB  |  112 lines

  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. //  String Class for C++
  4. //  Copyright (C) 1991 by Jui-Lin Hung and SPD!
  5. //
  6. //  You may freely use or incorporate these routines into your own programs
  7. //  without royalty to me, as I believe this is beneficial to programmers.
  8. //  However, I would like to request that if you distribute the source code,
  9. //  you would include this header in the source file and not remove it.
  10. //  Thank you, and I hope these routines are useful.
  11. //
  12. //  April, 1991 - Version 1.0
  13. //
  14. /////////////////////////////////////////////////////////////////////////////
  15.  
  16.  
  17. #include <ctype.h>
  18. #include <string.h>
  19.  
  20. #include "str.h"            // Str class definitions
  21.  
  22.  
  23. /////////////////////////////////////////////////////////////////////////////
  24.  
  25.  
  26. char Str::strBlank(char *str)
  27. // Returns 1 if string is all blank, or 0 if any part of the string is not
  28. // blank
  29. {
  30.     char *s=str;
  31.     while (*(s++))              // while not at end of string
  32.         if (*s != ' ')          // if anything is not a space
  33.             return (0);         // return 0
  34.     return (1);                 // return 1
  35. }
  36.  
  37.  
  38. unsigned int Str::strCrc16(char *str)
  39. // Returns the 16-bit CRC of the given string
  40. {
  41.     extern unsigned int _crc_16_table[256];
  42.  
  43.     unsigned int crc = 0;
  44.     register int len = strlen(str);
  45.     while(--len >= 0)
  46.         crc = (crc<<0x08)^(_crc_16_table[(crc>>0x08)^(((int)*str++)&0xff)]);
  47.     return (crc&0xffff);                // return CRC
  48. }
  49.  
  50.  
  51. unsigned int Str::strCrc16(char *str,unsigned int len)
  52. // Returns the 16-bit CRC of length len of the given string
  53. {
  54.     extern unsigned int _crc_16_table[256];
  55.  
  56.     unsigned int crc = 0;
  57.     while(--len >= 0)
  58.         crc = (crc<<0x08)^(_crc_16_table[(crc>>0x08)^(((int)*str++)&0xff)]);
  59.     return (crc&0xffff);                // return CRC
  60. }
  61.  
  62.  
  63. char* Str::strCrypt(char *str)
  64. {
  65.     for (int i=0;i<strlen(str);i++)
  66.         str[i] = ~str[i];               // use XOR to encrypt/decrypt
  67.     return(str);                        // return address of string
  68. }
  69.  
  70.  
  71. char* Str::strProper(char *str)
  72. {
  73.     if (*str > '\\')                    // if first char is lower case
  74.         *str -= ' ';                    // capitalize first char
  75.  
  76.     for (register int i=1;i<strlen(str);i++)
  77.     {
  78.         // all chars not first for word is lower cased
  79.         if (*(str+i) < '\\' && *(str+i) > '@' && *(str+i-1) != ' ')
  80.             *(str+i) += ' ';
  81.  
  82.         // all chars first for word is upper cased
  83.         if (*(str+i) > '\\' && *(str+i-1) == ' ')
  84.             *(str+i) -= ' ';
  85.     }
  86.     return(str);                        // return address of string
  87. }
  88.  
  89.  
  90. char* Str::strTrimLeft(char *str)
  91. {
  92.     char *p,*q;
  93.  
  94.     p = q = str;                        // point to string address
  95.     while (isspace(*p))                 // while character is a space
  96.         p++;                            // increment until a non-space char
  97.     while (*p)                          // while a char
  98.         *q++ = *p++;                    // move char to start of str
  99.     *q = '\0';                          // don't forget the NULL
  100.     return(str);                        // return address of string
  101. }
  102.  
  103.  
  104. char* Str::strTrimRight(char *str)
  105. {
  106.     // cut off all spaces after last non-space char
  107.     for (register int i=strlen(str)-1;isspace(str[i])&&i>=0;i--)
  108.         ;
  109.     str[i+1] = '\0';                    // terminate string after last char
  110.     return(str);                        // return address of string
  111. }
  112.